package sjjg.graph;

import java.util.Stack;

public class TuoPuLogical {//拓扑排序
       private Stack<Integer>order;
       TuoPuLogical(Digraph digraph){
           DirectedCycle directedCycle = new DirectedCycle(digraph);
           if(directedCycle.hasCycle()){
               return;
           }
           DepthFirstOrder depthFirstOrder = new DepthFirstOrder(digraph);
           order = depthFirstOrder.reversePost();
       }
       public boolean hasCycle(){
            return order==null;
       }
       public Stack<Integer>order(){
             return order;
       }

    public static void main(String[] args) {
        Digraph digraph = new Digraph(6);
        digraph.addEdge(0,2);
        digraph.addEdge(0,3);
        digraph.addEdge(2,4);
        digraph.addEdge(3,4);
        digraph.addEdge(4,5);
        digraph.addEdge(1,3);
        Stack<Integer> order = new TuoPuLogical(digraph).order;
        while (!order.empty()){
            System.out.print(order.pop());
        }
    }
}
